<?php
namespace pfp\lib {
    
    
    \defined( '_JEXEC' ) or die( 'Restricted access' );

    /**
     * Description of pfp
     * 
     * @todo: 
     * 
     * @author George Zeakis<zeageorge@google.com>
     */
    class pfp {

        /**
         *
         * @var DatabaseAdapterInterface
         */
        protected $_db = \NULL;

        /**
         *
         * @var pfp_Router
         */
        protected $_router = \NULL;

        /**
         *
         * @var User
         */
        protected $_user = \NULL;

        /**
         *
         * @var deepDynamicContainer
         */
        protected $_config = \NULL;

        /**
         *
         * @var array
         */
        protected $_events = array();

        /**
         *
         * @var array
         */
        protected $_vars = \NULL;

        /**
         * 
         * @param deepDynamicContainer $configuration
         * @return pfp Returns pfp object or throws an codeExecution_FlowControlException(array('return'=>FALSE)) on error
         */
        public function __construct(deepDynamicContainer $configuration) {
            $tmp = __FUNCTION__."_tmp"; if (isset($this->_vars["$tmp"])){ unset($this->_vars["$tmp"]); }
            try{

                $this->_config = $configuration;
                exceptionsManager::Init($this);

                eventManager::Init($this);
                $this->registerEvents();

                permsManager::Init($this->_config->pfp->permissionsManager);

                //   this should be implemented as an eventAction !!!     Logger::Init($this);

                $eventData = array('event_name'=>$this->_events['event_onStart'],'args'=>array('pfp_config'=>&$this->_config),); eventManager::riseEvent($eventData);

    //            $t = array(   //  This is a http_GET_Request
    //                array(   // program
    //                    'r',           // type: string (s), regex (r)
    //                    '/.*?/',       // value: depending on type
    //                ), 
    //                array(   // controller
    //                    'r',           // type: string (s), regex (r)
    //                    '/.*?/', // value: depending on type
    //                ),
    //                array(   // action
    //                    'r',           // type: string (s), regex (r)
    //                    '/.*?/',       // value: depending on type
    //                ),
    //                array(   // params
    //                    'r',           // type: string (s), regex (r)
    //                    '/.*?/', // value: depending on type
    //                ),
    //            );
    //            echo "<pre>";
    //            echo json_encode($t)."<br />\n";
    //            var_dump($t);
    //            die();

                $this->_db = new DatabaseAdapter();

                $this->_user = new User(array('storage'=>$this->_config->pfp->storage,
                                              'config' =>$this->_config->pfp->users));

                $this->_router = new pfp_Router(array('config'=>$this->_config->pfp->router, 
                                                      'paths' =>$this->_config->pfp->paths, 
                                                      'pfp'   =>$this));
                $this->_router->route();

            } catch (codeExecution_FlowControlException $exc) {
                $this->_vars["$tmp"]["errors"] = $exc;
                $eventData = array('event_name'=>$this->_events['event_onError'],'args'=>&$this->_vars); eventManager::riseEvent($eventData);
                unset($this->_vars["$tmp"]); 
                throw new codeExecution_FlowControlException(array('return'=>\FALSE), $exc->getMessage());
            }
        }

        private function registerEvents(){
            $this->_events = array(
                    'event_onStart'=>'event_onStart',
                    'event_onError'=>'event_onError',
                );
            eventManager::registerEvents($this->_events);
        }

        /**
         * 
         * @return DatabaseAdapterInterface
         */
        public function getDataBase(){ return $this->_db; }

        /**
         * 
         * @return pfp_Router
         */
        public function getRouter(){ return $this->_router; }

        /**
         * 
         * @return User
         */
        public function getUser(){ return $this->_user; }

        /**
         * 
         * @return deepDynamicContainer
         */
        public function getConfig(){ return $this->_config; }

        public function __destruct() { }

    }
}